package leetcode.editor.cn;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author: yangwk
 * @date: 2023/1/16
 * @description:
 **/
public class P2208MinimumOperationstoHalveArraySum {
    public static void main(String[] args) {
        System.out.println(halveArray(new int[]{5, 19, 8, 1}));
    }

    public static int halveArray(int[] nums) {
        double sum = 0;
        PriorityQueue<Double> priorityQueue = new PriorityQueue<>(new Comparator<Double>() {
            @Override
            public int compare(Double o1, Double o2) {
                return o1 > o2 ? -1 : (o1 == o2 ? 0 : 1);
            }
        });
        for (int num : nums) {
            sum += num;
            priorityQueue.add(Double.valueOf(num));
        }
        double halfSum = sum / 2;
        Double acm = 0d;
        int count = 0;
        while (acm < halfSum) {
            Double poll = priorityQueue.poll();
            Double i = poll / 2;
            acm += i;
            priorityQueue.add(i);
            count++;
        }
        return count;
    }
}
